import axios from "axios";

const  request = axios.create({
    baseURL: "http://localhost:8080/levelup",
    timeout: 3000
})

// request拦截器
// 对请求做统一管理
request.interceptors.request.use(config=>{
    config.headers['Content-Type'] = 'application/json;charset=utf-8';
    config.headers['token'] = 'token';
    return config;
},error => {
    console.error('request error: '+ error)
    return Promise.reject(error)
})

//response拦截器
request.interceptors.response.use(
    response => {
        let res = response.data;

        if(typeof res == 'string'){
            res = res ? JSON.parse(res) : res;
        }
        return res;
    },
    error => {
        console.error("response error: " + error)
        return Promise.reject(error)
    }
)

//导出，在其它View中可用
export default request